:-module(tg,[]).
:-use_module('de.pro').
:-use_module('wx.pro').


de:tg_items([甲,乙,丙,丁,戊,己,庚,辛,壬,癸]).
de:tengod_items([比肩,劫财,食神,伤官,偏财,正财,七杀,正官,枭神,正印]).

%% const
de:_.tg_jia():= E:- tg:new(E,0).
de:_.tg_yi():= E:- tg:new(E,1).
de:_.tg_bing():= E:- tg:new(E,2).
de:_.tg_ding():= E:- tg:new(E,3).
de:_.tg_wu():= E:- tg:new(E,4).
de:_.tg_ji():= E:- tg:new(E,5).
de:_.tg_geng():= E:- tg:new(E,6).
de:_.tg_xin():= E:- tg:new(E,7).
de:_.tg_ren():= E:- tg:new(E,8).
de:_.tg_gui():= E:- tg:new(E,9).

new(Var,V):-
	de:new(Var,[count-10,value-V,type-tg]).

wuxing(G,W):-
	new(G,GV),
	A is GV >> 1,
	W = G.wx_wood().add(A).

he(Gan1,Gan2,W):-
	new(Gan1,_),
	Gan2 = Gan1.add(5),
	A is Gan1.value mod 5,
	W = Gan1.wx_earth().add(A). 

yang(G,Y):-
	new(G,V),
	Y is 1 xor (V /\ 1).
yang(G):-
	yang(G,1).

brother(G1,G2):-
	yang(G1,Y1),
	A is Y1 * 2 - 1,
	G1.add(A) = G2.

%% 求第二个天干是第一个天干的什么，结果在第三个参数
tengod(G1,G2,R):- 
	yang(G1,D), yang(G2,E),
	wuxing(G1,W1),
	wuxing(G2,W2),
	C is (W2.value -  W1.value) mod 5,
	F is (D xor E) + C * 2,
	R = G1.get_item(tengod_items,F).

%% 同上，R 表示基本关系（见五行），Z 表示正偏，1正0偏
tengod(G1,G2,R,Z):-
	yang(G1,D), yang(G2,E),
	wuxing(G1,W1),
	wuxing(G2,W2),
	R is (W2.value -  W1.value) mod 5,
	Z is D xor E. 
